﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Film.Common;
using Film.Repository;


namespace PhuongDoAn.UC
{
    public partial class uc_advanceSearch : System.Web.UI.UserControl
    {
        int pageSize = 10;
        int pageSegSize = 1;
        public int count = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                initData();
                if (Request.QueryString["movie"] != null)
                {
                    txtMovieName.Text = Request.QueryString["movie"].Trim();
                }
                if (Request.QueryString["dir"] != null)
                {
                    txtDirector.Text = Request.QueryString["dir"].Trim();
                }

                if (Request.QueryString["year"] != null)
                {
                    cbYear.SelectedValue = Request.QueryString["year"].Trim();
                }

                if (Request.QueryString["genre"] != null)
                {
                    cbType.SelectedValue = Request.QueryString["genre"].Trim();
                }

            }

        }

        protected void Page_PreRender(object sender, EventArgs e)
        {

            try
            {

               Page.ClientScript.GetPostBackEventReference(this, string.Empty);

                int currentPage = 1;

                string eventTarget = this.Request["__EVENTTARGET"];
                string eventArgument = this.Request["__EVENTARGUMENT"];
                if (eventTarget != String.Empty && eventTarget == "pageIndex")
                {
                    currentPage = Convert.ToInt32(eventArgument);
                }

                //Response.Write(eventArgument);
                int start = pageSize * (currentPage - 1);

                List<tblMovieTemp> result = new List<tblMovieTemp>();
                tblMovieRepository movieRepository = new tblMovieRepository();


                result = movieRepository.Filter(txtMovieName.Text.Trim(), txtDirector.Text.Trim(), cbYear.SelectedValue, cbType.SelectedValue, start, pageSize, out count);

                rptData.DataSource = result;
                rptData.DataBind();

                if (count <= pageSize * 1)
                {
                    cbPaging.Visible = false;
                }
                else
                {
                    cbPaging.Items.Clear();
                    for (int i = 1; i <= (int)Math.Ceiling((double)count / pageSize); i++)
                    {
                        cbPaging.Items.Add(i.ToString());
                    }
                    cbPaging.SelectedValue = currentPage.ToString();
                }

            }
            catch (Exception ex)
            {
                Page.Response.Write("error");
            }
        }


        private void initData()
        {
            tblGenreRepository genreRepository = new tblGenreRepository();
            List<tblGenre> listAll = genreRepository.GetAll();
            listAll.Insert(0, new tblGenre() { ID = 0, Title = "--Tất cả--" });
            cbType.DataTextField = "Title";
            cbType.DataValueField = "Title";
            cbType.DataSource = listAll;
            cbType.DataBind();

            cbYear.Items.Add("--Tất cả--");
            for (int i = 1990; i < DateTime.Now.Year + 2; i++)
            {
                cbYear.Items.Add(i.ToString());
            }
        }

        public void btnSubmit_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?page=advs&movie=" + txtMovieName.Text.Trim() + "&dir=" + txtDirector.Text.Trim() + "&year=" + cbYear.SelectedItem.Value + "&genre=" + cbType.SelectedItem.Value);

        }
    }
}